package com.sg.business.permission.model;

import java.util.ArrayList;
import java.util.List;

public class ActionDefinition {

	public class CascadeActions {
		String cascadePermission;
		String[] cascadeActionCodes;
		String[] changedablePermission;
		String thisPermission;

		CascadeActions(String thisPermission,String targetPermission, String[] changedablePermission,
				String[] containedActionCodes) {
			this.thisPermission = thisPermission;
			this.cascadePermission = targetPermission;
			this.changedablePermission = changedablePermission;
			this.cascadeActionCodes = containedActionCodes;
		}

		public String[] getCascadeActionCodes() {
			return cascadeActionCodes;
		}

		public String getCascadePermission() {
			return cascadePermission;
		}
		
		public String[] getChangedablePermission() {
			return changedablePermission;
		}
		
		public String getThisPermission() {
			return thisPermission;
		}
	}

	public static final String F_ACTION_CODE = "actioncode";

	private String code;
	private String text;
	private String desc;
	private List<CascadeActions> cascadeActions;
	private String[] cascadeRegrantActionCodes;
	private String[] cascadePassDownActionCodes;
	private boolean editable;
	private boolean persistence;

	public ActionDefinition(String code, String text, String desc) {
		this.setCode(code);
		this.setText(text);
		this.setDesc(desc);
		editable = true;
		persistence = true;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public void addCascadeAction(String thisPermission,
			String targetPermission, String[] changeablePermissions,
			String[] cascadeActionCodes) {
		if (cascadeActions == null) {
			cascadeActions = new ArrayList<CascadeActions>();
		}
		cascadeActions.add(new CascadeActions(thisPermission,targetPermission,
				changeablePermissions, cascadeActionCodes));
	}

	public CascadeActions[] getCascadeActions(String thisPermission) {
		if (cascadeActions != null) {
			List<CascadeActions> result = new ArrayList<CascadeActions>();
			for (int i = 0; i < cascadeActions.size(); i++) {
				CascadeActions actions = cascadeActions.get(i);
				if(actions.getThisPermission().equals(thisPermission)){
					result.add(actions);
				}
			}
			if(!result.isEmpty()){
				return result.toArray(new CascadeActions[]{});
			}
		}
		return null;
	}

	public void setCascadeRegrantAction(String[] cascadeRegrantActionCodes) {
		this.cascadeRegrantActionCodes = cascadeRegrantActionCodes;
	}

	public String[] getCascadeRegrantActionCodes() {
		return cascadeRegrantActionCodes;
	}

	public void setCascadePassDownActions(String[] cascadePassDownActionCodes) {
		this.cascadePassDownActionCodes = cascadePassDownActionCodes;
	}

	public String[] getCascadePassDownActionCodes() {
		return cascadePassDownActionCodes;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setPersistence(boolean persistence) {
		this.persistence = persistence;
	}
	
	public boolean isPersistence() {
		return persistence;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ActionDefinition other = (ActionDefinition) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		return true;
	}
	
	
}
